import "reflect-metadata";

/**
 * 通用装饰器基类
 */
class MethodDecorator {
  static apply(
    method: string,
    url: string,
    options?: { cancelable?: boolean; noToken?: boolean; retry?: number }
  ) {
    return function (
      target: any,
      propertyKey: string,
      descriptor: PropertyDescriptor
    ) {
      Reflect.defineMetadata("httpMethod", method, target, propertyKey);
      Reflect.defineMetadata("url", url, target, propertyKey);
      Reflect.defineMetadata(
        "cancelable",
        options?.cancelable || false,
        target,
        propertyKey
      );
      Reflect.defineMetadata(
        "noToken",
        options?.noToken || false,
        target,
        propertyKey
      );
      Reflect.defineMetadata("retry", options?.retry || 0, target, propertyKey);
      return descriptor;
    };
  }
}

// 定义具体的 HTTP 方法装饰器
export const Get = (
  url: string,
  options?: { cancelable?: boolean; noToken?: boolean; retry?: number }
) => MethodDecorator.apply("GET", url, options);
export const Post = (
  url: string,
  options?: { cancelable?: boolean; noToken?: boolean; retry?: number }
) => MethodDecorator.apply("POST", url, options);
export const Put = (
  url: string,
  options?: { cancelable?: boolean; noToken?: boolean; retry?: number }
) => MethodDecorator.apply("PUT", url, options);
export const Delete = (
  url: string,
  options?: { cancelable?: boolean; noToken?: boolean; retry?: number }
) => MethodDecorator.apply("DELETE", url, options);
